package com.bearlair.ch8;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorSimpleDemo {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 4, 4,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(5), new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 0; i < 20; i++) {
            ThreadStart ts = new ThreadStart(i);
            threadPoolExecutor.execute(ts);
            TimeUnit.SECONDS.sleep(1);
        }
        threadPoolExecutor.shutdown();

    }

    static class ThreadStart implements Runnable {
        int i;

        public ThreadStart(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            System.out.println(getClass().getName() + " is running " + i);
        }
    }
}
